package com.hcy.rabbitmanagerapi.controller;

import com.hcy.manager.ManagerDriverCarInfoClient;
import com.hcy.manager.dto.request.DriverInfoAddRequestDTO;
import com.hcy.manager.dto.request.DriverInfoRequestDTO;
import com.hcy.manager.dto.response.DriverInfoDetailsResponseDTO;
import com.hcy.manager.dto.response.DriverInfoResponseDTO;
import com.hcy.rabbitbasicmodel.dto.MyPage;
import com.hcy.rabbitbasicmodel.enums.Clients;
import com.hcy.rabbitbasicmodel.enums.Log_Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.model.ReturnResult;
import com.hcy.rabbitbasicmodel.signl.Delete;
import com.hcy.rabbitbasicmodel.signl.Query;
import com.hcy.rabbitmanagerapi.utils.MyAutoOptLog;
import com.hcy.userclient.client.UserAuthClient;
import com.hcy.usermodel.dto.request.UserAuthRequestDTO;
import com.hcy.usermodel.dto.response.UserAuthsResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/4/28
 **/
@RestController
@Slf4j
@RequestMapping("/driver")
public class DriverController {
    @Autowired
    ManagerDriverCarInfoClient managerDriverCarInfoClient;
    @Autowired
    UserAuthClient userAuthClient;
    @Autowired
    PasswordEncoder passwordEncoder;

    @PostMapping("/getDriverList")
    public ReturnResult getDriverList(@Validated(Query.class) @RequestBody DriverInfoRequestDTO requestDTO) {
        log.info("getDriverList start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<MyPage<DriverInfoResponseDTO>> driverList = managerDriverCarInfoClient.getDriverList(requestDTO);
            if (driverList.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(driverList.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/cancelSig")
    @MyAutoOptLog(Log_Code.U_DRIVER)
    public ReturnResult cancelSig(@Validated(Delete.class) @RequestBody DriverInfoRequestDTO requestDTO) {
        log.info("cancelSig start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<Object> responseData = managerDriverCarInfoClient.cancelSig(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/addOrUpdateDriverInfo")
    @MyAutoOptLog(Log_Code.U_DRIVER)
    public ReturnResult addOrUpdateDriverInfo(@Validated @RequestBody DriverInfoAddRequestDTO requestDTO) {
        log.info("addOrUpdateDriverInfo start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            // 创建用户
            ResponseData<DriverInfoDetailsResponseDTO> responseData1 = managerDriverCarInfoClient.addOrUpdateDriverInfo(requestDTO);
            if (responseData1.getCode().noSuccess()) {
                return ReturnResult.fail("注册失败");
            }
            // 添加司机信息
            UserAuthRequestDTO userAuthRequestDTO = new UserAuthRequestDTO();
            userAuthRequestDTO.setBusinessCode(Clients.DRIVER.getName());
            String encode = passwordEncoder.encode(requestDTO.getPassword());
            userAuthRequestDTO.setPassword(encode);
            userAuthRequestDTO.setUsername(requestDTO.getPhoneNumber());
            userAuthRequestDTO.setNickname(requestDTO.getDriverName());
            ResponseData<UserAuthsResponseDTO> responseData = userAuthClient.insertOrUpdateUser(userAuthRequestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("注册失败");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/getDriverDetails")
    public ReturnResult getDriverDetails(@Validated(Delete.class) @RequestBody DriverInfoRequestDTO requestDTO) {
        log.info("getDriverDetails start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<DriverInfoDetailsResponseDTO> driverDetails = managerDriverCarInfoClient.getDriverDetails(requestDTO);
            if (driverDetails.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(driverDetails.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }
}
